﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestureSaver;
using System.IO;
using System.Globalization;

namespace Experimenter
{
    public static class Tester
    {
        private static string ConstructNewFileName()
        {
            int id = 0;
            string fileName = string.Empty;

            do
            {
                fileName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\TestResult_" + id.ToString("D2") + ".txt";
                id++;
            }
            while (File.Exists(fileName));

            return fileName;
        }

        public static void Test(GestureDB trainingDB, GestureDB testingDB, IAlgorithm algorithm)
        {
            string fileName = ConstructNewFileName();

            using (TextWriter writer = File.CreateText(fileName))
            {
                writer.WriteLine("Testing started.");
                writer.WriteLine(DateTime.Now);

                algorithm.Train(trainingDB);

                int totalTrueCount = 0;
                int totalFalseCount = 0;

                foreach (List<Gesture> gestureClass in testingDB)
                {
                    int classTrueCount = 0;
                    int classFalseCount = 0;

                    writer.WriteLine("\nCurrent class:" + gestureClass[0].gestureType);
                    
                    foreach (Gesture gestureSample in gestureClass)
                    {
                        GestureType result = algorithm.Recognize(gestureSample);
                        if (result == gestureSample.gestureType)
                        {
                            writer.Write("True. ");
                            classTrueCount++;
                            totalTrueCount++;
                        }
                        else
                        {
                            writer.Write("False. ");
                            classFalseCount++;
                            totalFalseCount++;
                        }

                        writer.WriteLine(gestureSample.inputFileName + "( Original: " + gestureSample.gestureType + " Calculated: " + result + " )");
                        writer.Flush();
                    }

                    writer.WriteLine();
                    double classAccuracy = (double)classTrueCount / (classTrueCount + classFalseCount);
                    writer.WriteLine("Class accuracy for " + gestureClass[0].gestureType + ": %" + (classAccuracy * 100).ToString(CultureInfo.InvariantCulture) + "\n");
                    writer.WriteLine();
                }

                writer.WriteLine();
                double totalAccuracy = (double)totalTrueCount / (totalTrueCount + totalFalseCount);
                writer.WriteLine("\nTotal accuracy of this algorithm: %" + (totalAccuracy * 100).ToString(CultureInfo.InvariantCulture) + "\n");

            }
        }
    }
}
